package com.hngy.xpq.shoppingcartmodule.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hngy.xpq.shoppingcartmodule.R;
import com.hngy.xpq.shoppingcartmodule.assets.XpqIcoMoon;
import com.hngy.xpq.shoppingcartmodule.assets.XpqIcoMoonUtils;
import com.hngy.xpq.shoppingcartmodule.bean.Cart;
import com.hngy.xpq.shoppingcartmodule.database.SqlCart;
import com.hngy.xpq.shoppingcartmodule.tool.alg;

import java.util.ArrayList;
import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.ViewHolder>{

    private List<Cart> listData = new ArrayList<>();
    private int number;
    private Button cart_z_price;

    static class ViewHolder extends RecyclerView.ViewHolder {

        View fileView;
        TextView name, price, number;
        Button reduce, add, delete;
        SqlCart sqlCart;

        public ViewHolder(View itemView) {
            super(itemView);
            fileView = itemView;
            name = itemView.findViewById(R.id.cart_shop_name);
            price = itemView.findViewById(R.id.cart_shop_price);
            number = itemView.findViewById(R.id.cart_Number);
            reduce = itemView.findViewById(R.id.cart_reduce);
            add = itemView.findViewById(R.id.cart_add);
            delete = itemView.findViewById(R.id.delete_cart);
        }
    }

    // 获取到数据
    public CartAdapter(List<Cart> list, Button cart_z_price) {
        this.listData = list;
        this.cart_z_price = cart_z_price;
    }

    @NonNull
    @Override
    public CartAdapter.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart,parent,false);
        final CartAdapter.ViewHolder holder = new CartAdapter.ViewHolder(view);
        // 添加数量
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {   // 防止输入非int类型数据
                    number = Integer.valueOf(holder.number.getText().toString());
                } catch (NumberFormatException e) {
                    Toast.makeText(holder.itemView.getContext(), "请输入正确的数量!", Toast.LENGTH_SHORT).show();
                    number = 1;
                    holder.number.setText(String.valueOf(number));
                }
                if (number > 100) {
                    Toast.makeText(holder.itemView.getContext(), "最多只可购买100份!", Toast.LENGTH_SHORT).show();
                } else {
                    number++;
                    holder.sqlCart = new SqlCart(parent.getContext());  // 开启数据库
                    int position = holder.getAdapterPosition();
                    listData.get(position).setNumber(number);   // 修改数量
                    cart_z_price.setText("结算: ￥ " + String.format("%.02f", alg.CountZPrice(listData).get("price")));
                    if (!holder.sqlCart.update(listData.get(position))) {   // 更新数据信息
                        Toast.makeText(holder.itemView.getContext(), "添加出现问题!", Toast.LENGTH_SHORT).show();
                    }
                    holder.number.setText(String.valueOf(number));
                }
            }
        });
        // 减少数量
        holder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {   // 防止输入非int类型数据
                    number = Integer.valueOf(holder.number.getText().toString());
                } catch (NumberFormatException e) {
                    Toast.makeText(holder.itemView.getContext(), "请输入正确的数量!", Toast.LENGTH_SHORT).show();
                    number = 1;
                    holder.number.setText(String.valueOf(number));
                }
                if (number <= 1) {
                    Toast.makeText(holder.itemView.getContext(), "当前为最小值!", Toast.LENGTH_SHORT).show();
                } else {
                    number--;
                    holder.sqlCart = new SqlCart(parent.getContext());  // 开启数据库
                    int position = holder.getAdapterPosition();
                    listData.get(position).setNumber(number);   // 修改数量
                    cart_z_price.setText("结算: ￥ " + String.format("%.02f", alg.CountZPrice(listData).get("price")));
                    if (!holder.sqlCart.update(listData.get(position))) {   // 更新数据信息
                        Toast.makeText(holder.itemView.getContext(), "添加出现问题!", Toast.LENGTH_SHORT).show();
                    }
                    holder.number.setText(String.valueOf(number));
                }
            }
        });
        // 移出购物车
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当前list
                int position = holder.getAdapterPosition();
                holder.sqlCart = new SqlCart(parent.getContext());
                if (holder.sqlCart.delete(listData.get(position).getId())) {
                    listData.remove(position);
                    cart_z_price.setText("结算: ￥ " + String.format("%.02f", alg.CountZPrice(listData).get("price")));
                    notifyDataSetChanged();
                    Toast.makeText(holder.itemView.getContext(), "商品:" + listData.get(position).getShop().getName() + "成功移出购物车!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(holder.itemView.getContext(), "移出失败!", Toast.LENGTH_SHORT).show();
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull CartAdapter.ViewHolder holder, int position) {
        // 数据绑定
        final Cart c = listData.get(position);
        holder.name.setText(c.getShop().getName());
        holder.price.setText("￥ " + c.getShop().getPrice());
        holder.number.setText(String.valueOf(c.getNumber()));
        holder.delete.setText(XpqIcoMoon.DELETE);
        XpqIcoMoonUtils.setIOC(holder.delete);
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }
}
